package com.littleHan.brushQuestions.web.uploadTopic.service.impl;

import com.littleHan.brushQuestions.exception.FailedToInsertException;
import com.littleHan.brushQuestions.exception.FailedToLoadException;
import com.littleHan.brushQuestions.utils.StringUtil;
import com.littleHan.brushQuestions.web.start.pojo.DicValue;
import com.littleHan.brushQuestions.web.start.pojo.Topic;
import com.littleHan.brushQuestions.web.uploadTopic.dao.UploadTopicDao;
import com.littleHan.brushQuestions.web.uploadTopic.service.UploadTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UploadTopicServiceImpl implements UploadTopicService {

    @Autowired
    private UploadTopicDao uploadTopicDao;

    @Override
    public Map<String, Object> getTopicAbout() throws FailedToLoadException{
        List<DicValue> typeList=uploadTopicDao.getType();
        List<DicValue> objectList=uploadTopicDao.getObject();
        List<DicValue> difficultList=uploadTopicDao.getDifficult();
        if(typeList.isEmpty() || objectList.isEmpty() || difficultList.isEmpty()){
            throw new FailedToLoadException("获取题目类型学科难度列表失败");
        }
        Map<String,Object> map=new HashMap<>();
        map.put("typeList",typeList);
        map.put("objectList",objectList);
        map.put("difficultList",difficultList);
        return map;
    }

    @Override
    @Transactional(rollbackFor = {FailedToInsertException.class})
    public void goUploadTopic(Topic topic,String userId) throws FailedToInsertException {
        topic.setId(StringUtil.generateUUID());
        topic.setTime(StringUtil.getSysTime());
        topic.setUserId(userId);
        topic.setIsCheck(0);
        int result=uploadTopicDao.insertUploadTopic(topic);
        if(result<1){
            throw new FailedToInsertException("添加题目失败");
        }
    }
}
